package com.tinywind.Sword.Dto;

import java.util.List;

/**
 * 封装json对象，所有返回结果都使用它
 */
public class Result<T> {
	// 设为private的不会显示出来.
	// 返回代码 0为成功,其余为各种异常.
	public int code;
	// 成功时返回的数据
	public Data<T> data;
	// 错误信息
	public String message;

//	public int itemCount;

	public Result(int code, Data<T> data, int itemCount, String message) {
		this.code = code;
		this.message = message;
//		this.itemCount = 1;
		this.data = data;
	}

	// 成功时无需返回结果的构造器
	public Result(int code) {
		this.code = code;
		if (code == 0) {
			this.message = "操作成功";
		} else {
			this.message = "操作失败";
		}
		this.data = null;
//		this.itemCount = 0;
	}

	// 成功时的构造器
	public Result(int code, Data<T> data) {
		this.code = code;
		this.data = data;
		this.message = "操作成功";
//		this.itemCount = 0;
	}
	
	// 成功时的构造器
	public Result(Data<T> data) {
		this.code = 0;
		this.data = data;
		this.message = "操作成功";
//		this.itemCount = 0;
	}

	// 1 成功时的构造器
	// 1.1 成功时的构造器
	public Result(int code, Data<T> data, int itemCount) {
		this.code = code;
		this.data = data;
		this.message = "操作成功";
//		this.itemCount = itemCount;
	}

	// 1.2 返回单个item的构造器
	public Result(int code, T item) {
		this.code = code;
		this.data = new Data<T>(item);
		this.message = "操作成功";
//		this.itemCount = 1;
	}

	// 1.3 返回多个item的构造器
	public Result(int code, List<T> items, Page page) {
		this.code = code;
		this.data = new Data<T>(items, page);
		this.message = "操作成功";
//		this.itemCount = items.size();
	}

	// 错误时的构造器,code + error
	public Result(int code, String error) {
		this.code = code;
		this.message = error;
		this.data = null;
	}
	//返回无数据返回结果.
//	public static Result errorResult(int code,String message) {
//		Result<> result = new Result(-1);
//		result.message = message;
//		return result;
//		
//	}
	public Result(T item) {
		Data<T> data = new Data<T>(item);
		this.code = 0;
		this.data = data;
		this.message = "操作成功";
	}

}
